#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
except:
    pass
import jarryLibs.mayakit.MayaUtils as mu

reload(mu)
import elsLibs.ShotInfoFromFile as elssif

reload(elssif)

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckELSCamera(CheckBase):
    def __init__(self):
        super(CheckELSCamera, self).__init__()
        pass

    def init_file(self):
        scene_name = mu.sceneName()

        sif = elssif.ShotInfoFromFile(scene_name)

        # self.shot_name = os.path.basename(scene_name).split('_')[1]

        self.cam_match_name = "%s_camera" % sif.get_seqshot

    def check_cam_is_lock(self, cam):
        lockAttr = [u'translateX', u'translateY', u'translateZ', u'rotateX', u'rotateY', u'rotateZ', u'scaleX',
                    u'scaleY', u'scaleZ']
        camAttr = cmds.listAttr(cam, l=True)
        if not camAttr:
            return False
        isLocked = set(camAttr) >= set(lockAttr)

        if isLocked:
            return True
        else:
            return False

    def lockattr(self, node):
        for attr in ['tx', 'ty', 'tz', 'rx', 'ry', 'rz', 'sx', 'sy', 'sz']:
            object = node + '.' + attr
            try:
                cmds.setAttr(object, l=1)
            except:
                pass

    def check(self):

        current_file = mu.sceneName()
        if not current_file:
            result_log = u'Error: 当前文件未保存!'
            return result_log
        if current_file.startswith('els_'):
            result_log = u'Error: 当前文件非els项目镜头规则!'
            return result_log
        try:
            self.init_file()
        except:
            result_log = u'Error:当前文件不符合ELS项目资产文件命名规则'
            return result_log

        result_proc = mu.check_default_cam()
        right_cam = []
        more_cam = []
        for c in result_proc:
            if c == self.cam_match_name:
                right_cam.append(c)
            else:
                more_cam.append(c)

        # log.info(right_cam)
        if right_cam and more_cam:
            result_log = u'Warning:查询到正确的相机:%s;' % right_cam
            if not self.check_cam_is_lock(right_cam[0]):
                result_log += u'<br><font color=\"#FF0000\">----------------------------检查到相机没有锁;</font>'
            result_log += u'<br>但是也查询到多余的相机:%s' % more_cam
            return result_log

        if right_cam:
            result_log = u'Succeed:查询到和文件名匹配的相机%s' % right_cam
            if not self.check_cam_is_lock(right_cam[0]):
                result_log += u'<br><font color=\"#FF0000\">----------------------------检查到相机没有锁;</font>'
                result_log = result_log.replace('Succeed', 'Warning:')
        elif more_cam:
            result_log = u'Error:查询到不匹配的相机%s' % more_cam
        else:
            result_log = u'Error:没有查询到相机!'
        return result_log

    def fix(self):
        self.init_file()
        self.lockattr(self.cam_match_name)
        return self.check()